package com.shiwaixiangcun.monitor.security;

import com.shiwaixiangcun.monitor.domain.User;
import com.shiwaixiangcun.monitor.utils.MsConstants;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtils {

    public static User getCurrentUser() {
        Authentication authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (authentication == null || authentication instanceof AnonymousAuthenticationToken) {
            User user = new User();
            user.setId(MsConstants.DEFAULT_CREATE_BY);
            user.setOrganizationId(MsConstants.DEFAULT_ORG_ID);
            return user;
        }

        MsUserDetails userDetails = (MsUserDetails) authentication
                .getPrincipal();
        return userDetails.getUser();
    }

}
